﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class delinfo : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString());
    static string infotype = "";
    static int show = -1;


    protected void Page_Load(object sender, EventArgs e)
    {
        string type = Request.QueryString["id"].ToString();

        string sqlstr = "select * from info where type='" + type + "'";
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        if (!IsPostBack)
        {
            infotype = Request.QueryString["id"].ToString();
            Gridviewbind(infotype);
        }
    }
    public void Gridviewbind(string type)
    {
        string sqlstr = "select * from info where type='" + type + "'";
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();

    }
    private void selectinfo(string type, bool show)
    {
        string sqlstr = "select * from info where type='" + type + "' and show='" + show + "' order by date desc";
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "id" };
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[6].Text == "False")
            {
                e.Row.Cells[6].Text = "<span style='color:red'>" + "未审核" + "</span>";
            }
            else
            {
                e.Row.Cells[6].Text = "<span style='color:blue'>" + "已审核" + "</span>";
            }
        }

    }
    protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
    {
        selectinfo(infotype, true);
        show = 1;
    }
    protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
    {
        selectinfo(infotype, false);
        show = 0;
    }
    protected void RadioButton3_CheckedChanged(object sender, EventArgs e)
    {
        Gridviewbind(infotype);
        show = -1;
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        updateInfo(id, infotype);
        switch (show)
        {
            case -1:
                Gridviewbind(infotype);
                break;
            case 0:
                selectinfo(infotype, false);
                break;
            case 1:
                selectinfo(infotype, true);
                break;
        }
    }
    private void updateInfo(string id, string type)
    {
        DataSet ds = selectInfo1(type, Convert.ToInt32(id));
        bool checkState = Convert.ToBoolean(ds.Tables[0].Rows[0][6].ToString());
        string sqlstr = "";
        if (checkState)
        {
            sqlstr = "update tinfo set show = 0 where id=" + id + "";
        }
        else
        {
            sqlstr = "update info set show = 1 where id=" + id + "";
        }
        conn.Open();
        SqlCommand comm = new SqlCommand(sqlstr, conn);
        comm.ExecuteNonQuery();
        conn.Close();

    }
    private DataSet selectInfo1(string type, int id)
    {
        string sqlstr = "select * from info where type='" + type + "' and id='" + id + "' order by date desc";
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string del_sql = "delete from info where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
        conn.Open();
        SqlCommand comm = new SqlCommand(del_sql, conn);
        comm.ExecuteNonQuery();
        conn.Close();
        Gridviewbind(infotype);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
}